function GameObject2(id) {
    this.x = 0;
    this.y = 0;
    this.id = id;

}
GameObject2.method('setID', function (value) {
    this.id = value;
    return this;
});

GameObject2.method('getID', function () {
    return this.id;
});

GameObject2.method('setPos', function (x_abs, y_abs) {
    this.x = x_abs * OurGame.grid;
    this.y = y_abs * OurGame.grid;
});

GameObject2.method('getPosX', function () {
    return Math.ceil(this.x / OurGame.grid);

});
GameObject2.method('getPosY', function () {
    return Math.ceil(this.y / OurGame.grid);

});
GameObject2.method('draw', function () {
    alert("DRAW should be overridden!");

});


function Player(id) {
    this.setID(id);

    this.x_move = 0;
    this.y_move = 0;
    this.type = PlayerType.HUMAN;
    this.speed = 10;
    this.lives = 3;
    this.kills = 0;
    this.maxBombsNumber = 3;
    this.currentBombsNumber = 0;
    this.assingedKeys = null;
    this.immortalTimerTime = 120;
    this.immortalTimer = null;
    this.immortalTimer = this.immortalTimerTime;
}

Player.inherits(GameObject2);

Player.method('moveX', function (n) {
    this.x_move += n * OurGame.grid;
});

Player.method('moveY', function (n) {
    this.y_move += n * OurGame.grid;
});

Player.method('isMoving', function () {
    return (this.x_move != 0 || this.y_move != 0);
});

Player.method('isAlive', function () {
    return this.lives > 0;

});
Player.method('bombExploded', function () {
    this.currentBombsNumber--;
});

Player.method('getActiveBombsCount', function () {
    return this.currentBombsNumber;
});
Player.method('removeLife', function () {
    var lifeWasRemoved = false;
    if (!this.lives == 0 && this.immortalTimer > this.immortalTimerTime) {
        this.lives--;
        updatePlayerSidebar(this);
        lifeWasRemoved = true;
    }
    this.immortalTimer = 0;
    return lifeWasRemoved;

});

Player.method('addKills', function () {
    this.kills++;
    updatePlayerSidebar(this);
});

Player.method('isCurrentPlaceFree', function () {
    var result = getItemIndexByPos(OurGame.bombs, this.getPosX(), this.getPosY());
    return result == -1 ? true : false;
});

Player.method('plantBomb', function () {
    if (this.isCurrentPlaceFree()) {
        // undefined bombs will be overwrote by active bomb
        var bombAdded = false;
        var result = OurGame.bombs.indexOf(undefined);
        if (result != -1) {
            OurGame.bombs[result] = new Bomb(bombUniqueID(), this.getPosX(), this.getPosY(), this);
            bombAdded = true;
        }
        if (!bombAdded) {
            OurGame.bombs.push(new Bomb(bombUniqueID(), this.getPosX(), this.getPosY(), this));
        }
        this.currentBombsNumber++;
    }
});

Player.method('assignKeys', function (keys) {
    this.assingedKeys = keys;
});

Player.method('draw', function () {
    if (this.immortalTimer < this.immortalTimerTime && Math.round(this.immortalTimer / 10) % 2 == 0) {
        return;
    }

    try {
        OurGame.ctx.drawImage(OurGame.images["spritesheet1"], OurGame.maxGrid * this.id, 4 * OurGame.maxGrid , OurGame.maxGrid, OurGame.maxGrid, this.x - OurGame.offSet, this.y - OurGame.offSet, OurGame.maxGrid, OurGame.maxGrid);
    } catch(e) {
        alert(e);
    }
});

Player.method('update', function (index) {
//    console.log("updating");
    this.immortalTimer++;
    var isChanged = false;
    var old_x = this.x;
    var old_y = this.y;
    if (this.x_move > 0) {
        this.x_move -= this.speed;
        this.x += this.speed;
        if (this.x_move < 0) {
            this.x += this.x_move;
            this.x_move = 0;
        }
        isChanged = true;
        updateGridMatrix(old_x, old_y, index);
    }
    if (this.x_move < 0) {
        this.x_move += this.speed;
        this.x -= this.speed;
        if (this.x_move > 0) {
            this.x += this.x_move;
            this.x_move = 0;
        }
        isChanged = true;
        updateGridMatrix(old_x, old_y, index);
    }
    if (this.y_move > 0) {
        this.y_move -= this.speed;
        this.y += this.speed;
        if (this.y_move < 0) {
            this.y += this.y_move;
            this.y_move = 0;
        }
        isChanged = true;
        updateGridMatrix(old_x, old_y, index);
    }
    if (this.y_move < 0) {
        this.y_move += this.speed;
        this.y -= this.speed;
        if (this.y_move > 0) {
            this.y += this.y_move;
            this.y_move = 0;
        }
        isChanged = true;
        updateGridMatrix(old_x, old_y, index);
    }
    return isChanged;
});

function updateGridMatrix(old_x, old_y, index) {
    if (level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)].charAt(1) === index) {
//        console.log("place1: " +  level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)]);
        level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)] = level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)].slice(2);
//        console.log(level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)]);
    } else if (level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)].charAt(3) === index) {
//        console.log("place2: " +  level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)]);
        level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)] = level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)].slice(0,2);
//        console.log(level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)]);
    } else {
//        console.log("removes");
        level.map[Math.floor(old_x / OurGame.grid)][Math.floor(old_y / OurGame.grid)] = "f0";
    }
}



